package poker;

import java.awt.Color;
import java.awt.Font;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.*;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;


public class HumanGui extends PlayerGui {
	private static final long serialVersionUID = 1L;
	public HumanGui(int i)	{
		setBackground(new Color(0, 139, 139));
		setFocusable(false);
		setLayout(null);
		switch(i){
	      case 0:
		    setBounds(192-160, 325, 240, 120);
		    break;
	      case 1:
	    	setBounds(192-160, 32, 240, 120);
		    break;
	      case 2:
		    setBounds(192+160, 32, 240, 120);
		    break;
	      case 3:
	    	setBounds(192+160, 325, 240, 120);
		    break;
	    
		}
	}
	
	 /** ZWRACANIE NAZWY GRACZA. */
	//  String getName(){
	 //   return("[" + playerName + "]");
	//  }
	  
	  /** ZWRACANIE WARTOSCI Z PRZEDZIALU [a,b] (WYBRANEJ PRZEZ GRACZA). */
	 // int getValue(int a, int b){
	   // int input=(-1);
	    //Scanner keyboard=new Scanner(System.in);
	    //try{
		 // do{ /** WCZYTUJEMY WARTOSC Z PRZEDZIALU [a,b]. */
		  //  System.out.print("[" + playerName + "]" + " Choose value (" + a + "-" + b + "): ");
		    //input=keyboard.nextInt();
		  //} while((input<a)||(input>b));
	    //} catch (Exception e){System.out.println("[" + playerName + "]" + " INVALID INPUT"); System.exit(0);} /** DLA NIEPOPRAWNEJ WARTOSCI PRZERYWAMY DZIALANIE. */
	    //return(input);
	  //}

	  /** ZWRACANIE TABLICY KART DO WYMIANY (WYBRANYCH PRZEZ GRACZA). */
	  int[] exchangeCards(int j, final TableGui table){
		  
		  final int[] toExchange;
			
		  
			int i, count = 0;
			//System.out.println("count:"+count);
			toExchange = new int[count];
			int k=0;
			for (i = 0; i < 5;i++ )	{
				if (table.DeckGui[j][i].hideornot())	{
						toExchange[k]=i+1;
						k++;
						//System.out.println("i:"+i);
					
				}		
			}

		return toExchange;
	  }
/*
	  /** ZWRACANIE NUMERU AKCJI W RUNDZIE LICYTACJI (WYBRANEJ PRZEZ GRACZA). 
	  /** 1-Check, 2-Bet, 3-Raise, 4-Call, 5-Fold, 6-All-In. 
	  int betAction(final Table table, boolean noBet, boolean allIn, boolean canCall, boolean canRaise){
	    int input=(-1),action;
	
	    /** MOZNA ZAGRAC: Check, Bet, Fold, All-In. 
		if(noBet){

		final JButton CheckButton = new JButton("CHECK");
		CheckButton.setBounds(0,60,60,30);
		CheckButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		CheckButton.setForeground(new Color(255, 140, 0));
		//CheckButton.setBackground(background);
		
		final JButton BetButton = new JButton("BET");
		BetButton.setBounds(60,60,60,30);
		//RaiseButton.setBackground(background);
		BetButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		BetButton.setForeground(new Color(255, 140, 0));
		final JButton FoldButton = new JButton("FOLD");
		FoldButton.setBounds(120, 60, 120, 24);
		FoldButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		FoldButton.setForeground(new Color(0, 0, 0));

		//FoldButton.setBackground(background);
		
		final JButton AllInButton = new JButton("ALL IN");
		AllInButton.setBounds(180,60,60,30);
		AllInButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		AllInButton.setForeground(new Color(255, 0, 0));
		//AllInButton.setBackground(background);
		add(FoldButton);
		add(BetButton);
		add(CheckButton);
		add(AllInButton);
		//TableGui.class.LoadValue(table.numberOfCurrentPlayer);
		CheckButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				remove(FoldButton);
				remove(BetButton);
				remove(CheckButton);
				remove(AllInButton);
				table.numberOfAction = 1;
				
			}
		});
		 BetButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				remove(FoldButton);
				remove(BetButton);
				remove(CheckButton);
				remove(AllInButton);
				table.numberOfAction = 2;
			
			}
		});
		 FoldButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					remove(FoldButton);
					remove(BetButton);
					remove(CheckButton);
					remove(AllInButton);
					table.numberOfAction = 5;
				
				}
			});
		AllInButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					remove(FoldButton);
					remove(BetButton);
					remove(CheckButton);
					remove(AllInButton);
					table.numberOfAction = 6;
				
				}
			});
		
		}
		/** MOZNA ZAGRAC: Raise, Call, Fold, All-In. */ /*
	else if((!noBet)&&(!allIn)&&(canCall)&&(canRaise)){
		System.out.println(" (1) Raise  (2) Call  (3) Fold (4) All-In ");
		
		final JButton RaiseButton = new JButton("RAISE");
		RaiseButton.setBounds(0,60,60,30);
		RaiseButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		RaiseButton.setForeground(new Color(255, 140, 0));
		//CheckButton.setBackground(background);
		final JButton CallButton = new JButton("CALL");
		CallButton.setBounds(60,60,60,30);
		//RaiseButton.setBackground(background);
		CallButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		CallButton.setForeground(new Color(255, 140, 0));
		final JButton FoldButton = new JButton("FOLD");
		FoldButton.setBounds(120, 60, 120, 24);
		FoldButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		FoldButton.setForeground(new Color(0, 0, 0));
		//FoldButton.setBackground(background);
		final JButton AllInButton = new JButton("ALL IN");
		AllInButton.setBounds(180,60,60,30);
		AllInButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		AllInButton.setForeground(new Color(255, 0, 0));
		//AllInButton.setBackground(background);
		add(FoldButton);
		add(CallButton);
		add(RaiseButton);
		add(AllInButton);
		RaiseButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				remove(FoldButton);
				remove(CallButton);
				remove(RaiseButton);
				remove(AllInButton);
				table.numberOfAction = 3;
				LoadValue(table);
			}
		});
		CallButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				remove(FoldButton);
				remove(CallButton);
				remove(RaiseButton);
				remove(AllInButton);
				table.numberOfAction = 4;
			
			}
		});
		 FoldButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					remove(FoldButton);
					remove(CallButton);
					remove(RaiseButton);
					remove(AllInButton);
					table.numberOfAction = 5;
				}
			});
		AllInButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					remove(FoldButton);
					remove(CallButton);
					remove(RaiseButton);
					remove(AllInButton);
					table.numberOfAction = 6;
				
				}
			});
		}
		/** MOZNA ZAGRAC: Call, Fold, All-In. 
		else if((!noBet)&&(!allIn)&&(canCall)&&(!canRaise)){
		System.out.println(" (1) Call  (2) Fold  (3) All-In ");
		final JButton CallButton = new JButton("CALL");
		CallButton.setBounds(0,60,60,30);
		CallButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		CallButton.setForeground(new Color(255, 140, 0));
		//CheckButton.setBackground(background);
		
		final JButton FoldButton = new JButton("FOLD");
		FoldButton.setBounds(180,60,60,30);
		FoldButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		FoldButton.setForeground(new Color(255, 0, 0));
		//AllInButton.setBackground(background);
		final JButton AllInButton = new JButton("ALL IN");
		AllInButton.setBounds(180,60,60,30);
		AllInButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		AllInButton.setForeground(new Color(255, 0, 0));
		//AllInButton.setBackground(background);
		add(FoldButton);
		add(CallButton);
		add(AllInButton);
		CallButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				remove(FoldButton);
				remove(CallButton);
				remove(AllInButton);
				table.numberOfAction = 4;

			}
		});
		 FoldButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				remove(FoldButton);
				remove(CallButton);
				remove(AllInButton);
				table.numberOfAction = 5;
			
			}
		});
		AllInButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					remove(FoldButton);
					remove(CallButton);
					remove(AllInButton);
					table.numberOfAction = 6;
				
				}
			});
		}
		/** MOZNA ZAGRAC: Fold, All-In. 
		else if((!noBet)&&(!allIn)&&(!canCall) || allIn){
		System.out.println(" (1) Fold  (2) All-In ");
		final JButton FoldButton = new JButton("FOLD");
		FoldButton.setBounds(180,60,60,30);
		FoldButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		FoldButton.setForeground(new Color(255, 0, 0));
		//AllInButton.setBackground(background);
		final JButton AllInButton = new JButton("ALL IN");
		AllInButton.setBounds(180,60,60,30);
		AllInButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		AllInButton.setForeground(new Color(255, 0, 0));
		//AllInButton.setBackground(background);
		add(FoldButton);
		add(AllInButton);
		 FoldButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					remove(FoldButton);
					remove(AllInButton);
					table.numberOfAction = 5;
				
				}
			});
		AllInButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				remove(FoldButton);
				remove(AllInButton);
				table.numberOfAction = 6;
				
			}
		});
		 
		}
	  return 0;
		
	  }

	  
	  int getValue(final Table table,int a, int b) {
		final JSlider slider = new JSlider();
		slider.setValue((int)(a-b)/2);
		slider.setMinimum(a);
		slider.setMaximum(b);
		slider.setBounds(30,60,180,30);
		add(slider);
		
		final JTextField actual = new JTextField(""+slider.getValue());
		actual.setBackground(new Color(240, 240, 240));
		actual.setFont(new Font("Tahoma", Font.BOLD, 13));
		actual.setBounds(0,60,30,30);
		add(actual);
		
		final JButton OK = new JButton("OK");
		OK.setBackground(new Color(240, 240, 240));
		OK.setFont(new Font("Tahoma", Font.BOLD, 13));
		OK.setBounds(210,60,30,30);
		add(OK);
		
		OK.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				table.betValue = Integer.parseInt(actual.getText());
				remove(OK);
				remove(slider);
				remove(actual); repaint();
			}
		});
		actual.addActionListener(new ActionListener() {
	        @Override
	        public void actionPerformed(ActionEvent e) {
		            	slider.setValue(Integer.parseInt(actual.getText()));
		            	table.betValue = Integer.parseInt(actual.getText());
		            	LoadValue(table);
	        }
			});
		slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                actual.setText(String.valueOf(slider.getValue()));
            }
		});
		 this.addFocusListener(new FocusListener() {
		        @Override
		        public void focusLost(FocusEvent e){
		        }

		        @Override
		        public void focusGained(FocusEvent e) {
		      
		        	actual.setText(String.valueOf(slider.getValue()));
		        
		        }
		    });
		 return 0;
		 
}
	  public void LoadValue (Table table)	{
		  switch(table.numberOfAction){
	      case 1:
		   // System.out.println(players[table.numberOfCurrentPlayer].getName() + "plays CHECK");
		    break;
          case 2:
        	//  table.betValue;//=players[numberOfCurrentPlayer].getValue(table,1,(table.playersChips[numberOfCurrentPlayer]-1));
		   // System.out.println(players[table.numberOfCurrentPlayer].getName() + " plays BET (" + table.betValue + ")");
		    table.tablePool+=  table.betValue;
		    table.playersChips[table.numberOfCurrentPlayer]-=  table.betValue;
		    table.playersBetsThisTurn[table.numberOfCurrentPlayer]+=  table.betValue;
		    if(  table.betValue>table.highestBid){table.highestBid=  table.betValue;}
		    table. actionsPerformed++;
		    break;
	      case 3:
	    	//  table.betValue;//=players[numberOfCurrentPlayer].getValue(table,(highestBid+1),(table.playersChips[numberOfCurrentPlayer]-1));
		  //  System.out.println(players[table.numberOfCurrentPlayer].getName() + " plays RAISE (" +   table.betValue + ")");
		    table.tablePool+=(  table.betValue-table.playersBetsThisTurn[table.numberOfCurrentPlayer]);
		    table.playersChips[table.numberOfCurrentPlayer]-=(  table.betValue-table.playersBetsThisTurn[table.numberOfCurrentPlayer]);
		    table.playersBetsThisTurn[table.numberOfCurrentPlayer]+=(  table.betValue-table.playersBetsThisTurn[table.numberOfCurrentPlayer]);
		    table.highestBid=  table.betValue;
		    table.actionsPerformed=1;
		    break;
	      case 4:
	    	  table.betValue=table.highestBid;
		   // System.out.println(players[table.numberOfCurrentPlayer].getName() + " plays CALL (" +   table.betValue + ")");
		    table.tablePool+=(  table.betValue-table.playersBetsThisTurn[table.numberOfCurrentPlayer]);
		    table.playersChips[table.numberOfCurrentPlayer]-=(  table.betValue-table.playersBetsThisTurn[table.numberOfCurrentPlayer]);
		    table. playersBetsThisTurn[table.numberOfCurrentPlayer]+=(  table.betValue-table.playersBetsThisTurn[table.numberOfCurrentPlayer]);
		    table. actionsPerformed++;
		    break;
	      case 5:
		    //System.out.println(players[table.numberOfCurrentPlayer].getName() + " plays FOLD");
		    table.playersActivity[table.numberOfCurrentPlayer]=0;
		    table.numberOfActivePlayers--;
		    break;
	      case 6:
	    	  table.betValue=table.playersChips[table.numberOfCurrentPlayer];
		   //System.out.println(players[table.numberOfCurrentPlayer].getName() + " plays ALL-IN (" + (  table.betValue+table.playersBetsThisTurn[table.numberOfCurrentPlayer]) + ")");
		    table.tablePool+=  table.betValue;
		    table.playersChips[table.numberOfCurrentPlayer]-=  table.betValue;
		    table. playersBetsThisTurn[table.numberOfCurrentPlayer]+=  table.betValue;
		    if(  table.betValue>table.highestBid){table.highestBid=  table.betValue;}
		    if(table.numberOfAllIns==0) table.actionsPerformed=0;
		    table.playedAllIn=true;
		    table.numberOfAllIns++;
		    table.actionsPerformed++;
		    break;
		 */   
        //}
		//System.out.println();
      
}
	  
//}